<template>
  <div class="search-result">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
      error-text="请求失败,点击重新加载">
      <van-cell v-for="(item,ind) in list" :key="ind" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
// 引入接口
import { getSearchResult } from '@/api/search.js'
export default {
  name: 'SearchResult',
  props: {
    q: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 当前页
      per_page: 20, // 每页数量
      error: false // 加载失败
    }
  },
  methods: {
    // 获取搜索结果
    async onLoad () {
      try {
        const { data: res } = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.q // 当前搜索关键字
        })
        console.log(res)
        const { results } = res.data
        this.list.push(...results)

        // 加载状态结束
        this.loading = false

        // 判断是否已经加载完毕
        if (results.length) {
          this.page++
        } else {
          // 没有数据  加载完毕
          this.finished = true
        }
      } catch (err) {
        // 加载失败  继续尝试
        this.error = true
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style lang='less' scoped>
</style>
